/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.smarttmt.idu.web.persistence.controller;

import com.smarttmt.idu.web.persistence.controller.exceptions.NonexistentEntityException;
import com.smarttmt.idu.web.persistence.controller.exceptions.PreexistingEntityException;
import com.smarttmt.idu.web.persistence.controller.exceptions.RollbackFailureException;
import com.smarttmt.idu.web.persistence.entity.Uscochip;
import com.smarttmt.idu.web.persistence.entity.UscochipPK;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.smarttmt.idu.web.persistence.entity.Usuacont;
import java.math.BigInteger;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.transaction.UserTransaction;

/**
 *
 * @author root
 */
public class UscochipJpaController implements Serializable {

    public UscochipJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Uscochip uscochip) throws PreexistingEntityException, RollbackFailureException, Exception {
        if (uscochip.getUscochipPK() == null) {
            uscochip.setUscochipPK(new UscochipPK());
        }
        uscochip.getUscochipPK().setUscocodi(new BigInteger(uscochip.getUsuacont().getUscocodi().toString()));
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Usuacont usuacont = uscochip.getUsuacont();
            if (usuacont != null) {
                usuacont = em.getReference(usuacont.getClass(), usuacont.getUscocodi());
                uscochip.setUsuacont(usuacont);
            }
            em.persist(uscochip);
            if (usuacont != null) {
                usuacont.getUscochipList().add(uscochip);
                usuacont = em.merge(usuacont);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findUscochip(uscochip.getUscochipPK()) != null) {
                throw new PreexistingEntityException("Uscochip " + uscochip + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Uscochip uscochip) throws NonexistentEntityException, RollbackFailureException, Exception {
        uscochip.getUscochipPK().setUscocodi(new BigInteger(uscochip.getUsuacont().getUscocodi().toString()));
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Uscochip persistentUscochip = em.find(Uscochip.class, uscochip.getUscochipPK());
            Usuacont usuacontOld = persistentUscochip.getUsuacont();
            Usuacont usuacontNew = uscochip.getUsuacont();
            if (usuacontNew != null) {
                usuacontNew = em.getReference(usuacontNew.getClass(), usuacontNew.getUscocodi());
                uscochip.setUsuacont(usuacontNew);
            }
            uscochip = em.merge(uscochip);
            if (usuacontOld != null && !usuacontOld.equals(usuacontNew)) {
                usuacontOld.getUscochipList().remove(uscochip);
                usuacontOld = em.merge(usuacontOld);
            }
            if (usuacontNew != null && !usuacontNew.equals(usuacontOld)) {
                usuacontNew.getUscochipList().add(uscochip);
                usuacontNew = em.merge(usuacontNew);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                UscochipPK id = uscochip.getUscochipPK();
                if (findUscochip(id) == null) {
                    throw new NonexistentEntityException("The uscochip with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(UscochipPK id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Uscochip uscochip;
            try {
                uscochip = em.getReference(Uscochip.class, id);
                uscochip.getUscochipPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The uscochip with id " + id + " no longer exists.", enfe);
            }
            Usuacont usuacont = uscochip.getUsuacont();
            if (usuacont != null) {
                usuacont.getUscochipList().remove(uscochip);
                usuacont = em.merge(usuacont);
            }
            em.remove(uscochip);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Uscochip> findUscochipEntities() {
        return findUscochipEntities(true, -1, -1);
    }

    public List<Uscochip> findUscochipEntities(int maxResults, int firstResult) {
        return findUscochipEntities(false, maxResults, firstResult);
    }

    private List<Uscochip> findUscochipEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Uscochip.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Uscochip findUscochip(UscochipPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Uscochip.class, id);
        } finally {
            em.close();
        }
    }

    public int getUscochipCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Uscochip> rt = cq.from(Uscochip.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
